package jsyf.oa.web.system;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ExportCustomerExcel;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.BigAchievementsFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/2 0002.
 */
@Api(value = "销售绩效考核接口", description = "销售绩效考核接口")
@Controller
@RequestMapping("/bigAchievements")
public class BigAchievementsController {
    @Resource
    private BigAchievementsFacade bigAchievementsFacade;

    @ResponseBody
    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "保存/修改",
            notes = "保存/修改",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "JsonObject:''", dataType = "String", paramType = "body")
    public void save(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        String records = bigAchievementsFacade.save(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
    }

    @ResponseBody
    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "逻辑删除",
            notes = "逻辑删除",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "id:''", dataType = "String", paramType = "body")
    public void delById(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = bigAchievementsFacade.delById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
    }
    @ResponseBody
    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "物理删除",
            notes = "物理删除",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "id:''", dataType = "String", paramType = "body")
    public void removeById(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = bigAchievementsFacade.removeById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
    }

    @ResponseBody
    @RequestMapping(value = "/getListByBigAreaId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询人员配置以及大区销售目标",
            notes = "查询人员配置以及大区销售目标",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "bigAreaId:''", dataType = "String", paramType = "body")
    public void getListByBigAreaId(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = bigAchievementsFacade.getListByBigAreaId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(records));
    }

    @ResponseBody
    @RequestMapping(value = "/getPeopleFineByCompanyId.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询团队组建考核罚款",
            notes = "查询团队组建考核罚款",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "companyId:'',day:''", dataType = "String", paramType = "body")
    public void getPeopleFineByCompanyId(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = bigAchievementsFacade.getPeopleFineByCompanyId(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(records));
    }

    @ResponseBody
    @RequestMapping(value = "/getForGroupVacantAndFine.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取整个集团空缺人数及罚款",
            notes = "获取整个集团空缺人数及罚款",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "day:''", dataType = "String", paramType = "body")
    public void getForGroupVacantAndFine(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = bigAchievementsFacade.getForGroupVacantAndFine(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(records));
    }

    @ResponseBody
    @RequestMapping(value = "/getRoleChangeDate.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取岗位结束日期",
            notes = "获取岗位结束日期",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "day:''", dataType = "String", paramType = "body")
    public void getRoleChangeDate(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject records = bigAchievementsFacade.getRoleChangeDate(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(records));
    }





    @RequestMapping(value = "/getDownByCondition.json", method = RequestMethod.GET)
    public void getDownByCondition(HttpServletResponse response, @RequestParam String companyId, @RequestParam String day) throws Exception {
        //查出来的数据
        List<JSONObject> jsonObjectList =null;
        jsonObjectList = bigAchievementsFacade.getDownByCondition(companyId,day);
        getDownExcel(jsonObjectList, response,day);
    }
    public void getDownExcel(List<JSONObject> jsonObjectList, HttpServletResponse response,String day) throws Exception {
        //导出表名、列名
        String month = day.substring(0, 7);
        String title = "团队组建考核报表"+"("+month+")";
        String[] rowsName = new String[]{ "公司名称","大区","责任人","当月罚款","区域经理标配","区域经理现有","区域经理空缺","大区经理标配", "大区经理现有","大区经理空缺"};
        List<Object[]> dataList = new ArrayList<Object[]>();
        Object[] objs = null;
        for (int i = 0; i < jsonObjectList.size(); i++) {
            JSONObject jsonObject = jsonObjectList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = jsonObject.getString("companyName");
            if(com.alibaba.dubbo.common.utils.StringUtils.isNotEmpty(jsonObject.getString("regionName"))){
                objs[1] = jsonObject.getString("regionName");
            }else {
                objs[1] = jsonObject.getString("roleName");
            }
            objs[2] = jsonObject.getString("userName");
            objs[3] = jsonObject.getString("money");
            objs[4] = jsonObject.getString("managerTargetNum");
            objs[5] = jsonObject.getString("managerCount");
            objs[6] = jsonObject.getString("managerVacant");
            objs[7] = jsonObject.getString("bigManagerTargetNum");
            objs[8] = jsonObject.getString("bigManagerCount");
            objs[9] = jsonObject.getString("bigManagerVacant");
            dataList.add(objs);
        }
        int cloumLength=rowsName.length;
        ExportCustomerExcel ex = new ExportCustomerExcel(title, rowsName, dataList, response);
        ex.exportContractAccount(cloumLength);
    }

}
